package com.example.indenture.race.dao;


import com.example.indenture.common.vo.Search;
import com.example.indenture.race.entity.Race;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface RaceDao {
    //插入比赛

    @Insert("insert into race (create_date,update_time,des,name,url,cid,deadline,branks,sranks,money)" +
            "values(#{createDate},#{updateTime},#{des},#{name},#{url},#{cid},#{deadline},#{branks},#{sranks},#{money})" )
    Integer insert(Race race);

    //
    @Select("select count(*) from race where name=#{name}")
    Integer selectIdByName(String name);

    //删除比赛

    @Delete("delete from race where id=#{rid} ")
    Integer delete(Integer rid);

    //更新比赛

    @Update("update race set update_time=#{updateTime},des=#{des},name=#{name},url=#{url},cid=#{cid},branks=#{branks},sranks=#{sranks},money=#{money} where name=#{name}")
    Integer update(Race race);


    //查询比赛
    @Select("SELECT * FROM race WHERE id=#{rid}")
    Race getRaceById(Integer rid);


    //获取所有的race
    @Select("<script>"
            + "select * from race "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='sort != \"\" and sort != null'>"
            + " Order by ${sort} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " Order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Race> getAllRaces(Search search);

    //根据user获取比赛
    @Select("select * from race where id in (select rid from team where id in (select tid from team_user where uid=#{uid}))")
    List<Race> getRacesByUser(Integer uid, Search search);
    @Select("select * from race where cid=#{cid}")
    List<Race> getRacesByCompany(Integer cid);

    @Select("select * from race")
    List<Race> getAllracess();


}
